local datas = require("core_kukude/data/insertobjs")

---comment
---@param prefab string
---@param data data_insertobj
---@return any
local function MakeObj(prefab, data)
    local assets = {}
    if not data.noimg then
        assets = {
            Asset( "ANIM", "anim/"..prefab..".zip"),
            Asset( "ATLAS", "images/inventoryimages/"..prefab..".xml" ),
        }
    end

    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        MakeInventoryPhysics(inst)

        inst.AnimState:SetBank(data.noimg and "goldnugget" or prefab)
        inst.AnimState:SetBuild(data.noimg and "gold_nugget" or prefab)
        inst.AnimState:PlayAnimation("idle")

        --植入物
        inst:AddTag("kkd_implant")        

        MakeInventoryFloatable(inst, "med", nil, 0.7)

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        --inst:AddComponent("stackable")
        --inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM

        inst:AddComponent("inspectable")

        inst:AddComponent("finiteuses")
        inst.components.finiteuses:SetOnFinished(function ()
            inst:DoTaskInTime(0, function()
                inst:Remove()
            end)
        end)

        inst:AddComponent("inventoryitem")
        inst.components.inventoryitem.imagename = data.noimg and 'goldnugget' or prefab
        inst.components.inventoryitem.atlasname = data.noimg and 'images/inventoryimages2.xml' or "images/inventoryimages/"..prefab..".xml"

        inst._onequip = data.onequip
        inst._onunequip = data.onunequip

        --[[inst.components.inventoryitem:SetOnPutInInventoryFn(function(inst, owner)
            if owner:HasTag("kkd_fleshsack") then 
                owner:DoTaskInTime(0, function()
                    local kkd = owner.components.inventoryitem:GetGrandOwner()
                    --if not kkd then return end 
                    data.onequip(inst, owner, kkd) 
                    local function onlose(self, losedata)
                        if losedata.prev_item == inst then
                            data.onunequip(inst, owner, kkd)
                            self:RemoveEventCallback("itemlose",onlose)
                        end
                    end
                    owner:ListenForEvent("itemlose", onlose)
                end)
            end
        end)]]

        MakeHauntableLaunchAndSmash(inst)

        if data.onsave then
            inst.OnSave = data.onsave
        end
        if data.onload then
            inst.OnLoad = data.onload
        end

        return inst
    end

    return Prefab(prefab, fn, assets, data.prefabs)
end

local prefabs = {}

for prefab, data in pairs(datas) do 
    table.insert(prefabs, MakeObj(prefab, data))
end

return unpack(prefabs)